package cn.itcast.dao.impl;

import cn.itcast.dao.BookDao;
import cn.itcast.domain.Book;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.SQLException;

@Repository
public class BookDaoImpl implements BookDao {
  @Autowired

  private QueryRunner qr;//连接数据库,@Autowired的new QueryRunner;

  @Autowired
  private Connection connection;
    public void save() {

        System.out.println("save ok");
    }

    @Override
    public Book findById(int id) {
        try {
            //ca
           return qr.query("select * from book where id = ? ",new BeanHandler<Book>(Book.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;//查不到返回null;
    }

  /*  @Override
    public void update(int id,double price) {
        try {
            qr.update("update book set price=? where id=?",price,id);
        } catch(SQLException e) {
            e.printStackTrace();
        }

    }*/
    public void update(int id,double price){
        try {
            qr.update(connection,"update book set price=? where id=?",price,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
